const ble = require('../ble.js')

const writeBuffer=(buffer)=> {
  return ble.write(buffer)
}

const readBuffer=(len)=> {
  return ble.read(len)
}

const waitMs=(ms)=> {
    return new Promise(resolve => {
        setTimeout(resolve, ms)
    })
}

const chunk=(list, size)=> {
    const rst = []
    for (let i = 0; i < list.length; i++) {
        if (i % size == 0) {
            rst.push([])
        }
        rst[rst.length - 1].push(list[i])
    }
    return rst
}

const patchLoad=async(pLutBytes)=> {
    const list = chunk(pLutBytes, 24)
    for (let i = 0; i < list.length; i++) {
        const patch=list[i]
        await writeBuffer([0x1b, ...patch])
    }
}

const tableWrite=async(tab)=> {
    for (let i = 0; i < tab.length; i++) {
        const item = tab[i]
        await tuner[item.method](item.data)
    }
}
const tuner={
  writeBuffer,
  readBuffer,
  waitMs,
  patchLoad,
  tableWrite,
}
module.exports = tuner